package guanqu.server;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.junit.Test;

import com.alibaba.fastjson.JSON;

import guanqu.beans.IrrBMSystem;
import guanqu.beans.RealTimeIrrBRWaterDam;
import manager.PersistentManager;

/**
 * Servlet implementation class GetRealWaterGateDam
 */
//@WebServlet("/GetRealWaterGateDam")
public class GetRealWaterGateDam extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetRealWaterGateDam() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//		response.getWriter().append("Served at: ").append(request.getContextPath());
		request.setCharacterEncoding("UTF-8");
		
		String stadiacode = request.getParameter("data");
		
		Download download = new Download();
		
		SessionFactory sf = PersistentManager.sharemanager().getSessionFactory();
//		Session session = sf.openSession();
		Session session = sf.getCurrentSession();
		
		Transaction tx = session.beginTransaction();
		try
		{
			// 获取闸门实时数据
			Query q = session.createQuery("from RealTimeIrrBRWaterDam _RealTimeIrrBRWaterDam where _RealTimeIrrBRWaterDam.stadiaCode=? order by _RealTimeIrrBRWaterDam.meaTime desc");
			q.setParameter(0, stadiacode);
			q.setMaxResults(1);
			List<RealTimeIrrBRWaterDam> dams = q.list();
			if (!dams.isEmpty()) {
				download.setRealTimeIrrBRWaterDam(dams.get(0));
			}
			
			// 获取照片实时数据
			Query q2 = session.createQuery("from IrrBMSystem irr where irr.stadiaCode=? order by irr.meaTime desc");
			q2.setParameter(0, stadiacode);
			q2.setMaxResults(1);
			List<IrrBMSystem> irrBMSystems = q2.list();
			if (!irrBMSystems.isEmpty()) {
				download.setIrrBMSystem(irrBMSystems.get(0));
			}
			
			tx.commit();
			download.setStatus(DownloadInfo.SUCCESS);
		}catch(Exception e)
		{
			download.setStatus(DownloadInfo.FAILED);
			tx.rollback();
			e.printStackTrace();
		}finally
		{
//			session.close();
		}
		
		response.setCharacterEncoding("UTF-8");
		System.out.println(JSON.toJSONString(download));
		response.getWriter().append(JSON.toJSONString(download));
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	
	
	// 下行数据
	public static class Download
	{
		
		int status;
		// 最新实时数据
		RealTimeIrrBRWaterDam realTimeIrrBRWaterDam;
		
		// 最新照片
		IrrBMSystem irrBMSystem;
		
		

		public int getStatus() {
			return status;
		}

		public void setStatus(int status) {
			this.status = status;
		}

		public RealTimeIrrBRWaterDam getRealTimeIrrBRWaterDam() {
			return realTimeIrrBRWaterDam;
		}

		public void setRealTimeIrrBRWaterDam(RealTimeIrrBRWaterDam realTimeIrrBRWaterDam) {
			this.realTimeIrrBRWaterDam = realTimeIrrBRWaterDam;
		}

		public IrrBMSystem getIrrBMSystem() {
			return irrBMSystem;
		}

		public void setIrrBMSystem(IrrBMSystem irrBMSystem) {
			this.irrBMSystem = irrBMSystem;
		}
		
		
	}
	
	
	@Test
	public void test0()
	{
		SessionFactory sf = PersistentManager.sharemanager().getSessionFactory();
		Session session = sf.openSession();
		
		Transaction tx = session.beginTransaction();
		try
		{
			Query q = session.createQuery("from RealTimeIrrBRWaterDam _RealTimeIrrBRWaterDam where _RealTimeIrrBRWaterDam.stadiaCode=? order by _RealTimeIrrBRWaterDam.meaTime desc");
			q.setParameter(0, "10510001");
			q.setMaxResults(1);
			List<RealTimeIrrBRWaterDam> dams = q.list();
			
			
			
			
			
			
			tx.commit();
		}catch(Exception e)
		{
			tx.rollback();
			e.printStackTrace();
		}finally
		{
			session.close();
		}
	}
	
	
	@Test
	public void test1()
	{
		SessionFactory sf = PersistentManager.sharemanager().getSessionFactory();
		Session session = sf.openSession();
		
		Transaction tx = session.beginTransaction();
		try
		{
			Query q2 = session.createQuery("from IrrBMSystem irr where irr.stadiaCode=? order by irr.meaTime desc");
			q2.setParameter(0, "10510001");
			q2.setMaxResults(1);
			List<IrrBMSystem> dams = q2.list();
			
			
			
			tx.commit();
		}catch(Exception e)
		{
			tx.rollback();
			e.printStackTrace();
		}finally
		{
			session.close();
		}
	}

}
